﻿#include "unitclass.h"
#include <QDebug>
sysConfig_t sysConfig;
int getDispFreeSpace()
{
    QString path="/media/sda1";
    struct statfs fs;
    char *disk =path.toLatin1().data();
    if(statfs(disk,&fs)<0)
    {
        debugOut(QString("%1  >>>>>scanDisk err!!!").arg(sysConfig.diskPath));
        return -1;
    }
    double disk_size = fs.f_blocks/1024*fs.f_bsize/1024;//MB总大小
    double avilable =fs.f_bavail/1024*fs.f_bsize/1024;//MB可以空间大小
//    double all=fs.f_blocks - fs.f_bfree + fs.f_bavail;
//    double percent = (fs.f_blocks - fs.f_bfree ) * 100 / all + 1;
    double percent=(avilable/disk_size)*100;
    return (int)percent;
}

int getMemFreeSpace()
{
    double percent=0;
    int total=0,free=0;
    QString fileName=QString("/proc/meminfo");
    QFile myfile(fileName);
    if(myfile.open(QIODevice::ReadOnly))
    {
        QString line = myfile.readLine();
        QString line2 = myfile.readLine();
        qDebug()<<line<<line2;
        QStringList l=line.split(":");
        total = l.at(1).simplified().remove("kB").toInt();
        QStringList l2=line2.split(":");
        free = l2.at(1).simplified().remove("kB").toInt();
        myfile.close();
    }
    percent = (total-free)*100/total;
    return (int)percent;
}

void  setDiskPath(QString path)
{
    sysConfig.diskPath = path;
}
void debugOut(QString str)
{
    qDebug()<< QString("%1 %2")
            .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz"))
            .arg(str);
}
void deleteOldLog()
{
    QString delename=QDate::currentDate().addDays(-7).toString("yyyyMMdd");
    QDir dir("/media/sda/log");
    QStringList filters;
    filters.append("*.txt");
    foreach (QFileInfo fileInfo, dir.entryInfoList(filters, QDir::Files))
    {
        QString baseName = fileInfo.completeBaseName();
        qDebug()<<"baseName="<<baseName;
        if(baseName<=delename)
        {
            QFile deleFile(fileInfo.absoluteFilePath());
            if(deleFile.exists())
                deleFile.remove();
        }
    }

}



